package com.huifaapp.mall.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huifaapp.mall.R;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.selectImage.SelectImageView;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import me.zhanghai.android.materialratingbar.MaterialRatingBar;

public class AssessActivity extends AppCompatActivity {

    private LinearLayout Ligoodass, Licenterass, Lilowass;
    private ImageView Imgoodass, Imcenterass, Imlowass;
    private ImageView Imback;
    private MaterialRatingBar materialRatingBar,materialRatingBar2,materialRatingBar3;
    private int mater=0,mater2=0,mater3=0;
    private int ass = 0;
    private EditText ed_content;
    private TextView send;
    private String text;
    SelectImageView selectImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assess);
        initView();
        initData();
    }


    private void initView() {
        Ligoodass = (LinearLayout) findViewById(R.id.Lineargood);
        Licenterass = (LinearLayout) findViewById(R.id.Linearcenter);
        Lilowass = (LinearLayout) findViewById(R.id.Linearlow);
        Imgoodass = (ImageView) findViewById(R.id.Imgood);
        Imcenterass = (ImageView) findViewById(R.id.Imcenter);
        Imlowass = (ImageView) findViewById(R.id.Imlow);
        Imback = (ImageView) findViewById(R.id.iv_assess_back);
        materialRatingBar = (MaterialRatingBar) findViewById(R.id.assess_mater);
        materialRatingBar2 = (MaterialRatingBar) findViewById(R.id.assess_mater2);
        materialRatingBar3 = (MaterialRatingBar) findViewById(R.id.assess_mater3);
        ed_content = (EditText) findViewById(R.id.assess_content);
        send = (TextView) findViewById(R.id.assess_send);

        selectImageView = new SelectImageView();
        selectImageView.setMaxNum(3);
        getSupportFragmentManager().beginTransaction().add(R.id.fragmentContent, selectImageView).commit();
        //注意 获取选择的照片
        //这是一个图片选择数组，里面有图片的信息，宽高，是否是GIF，本地路径等信息
        //到时候上传图片 用下面的方法获取选择的图片
        //selectImageView.getPathList();
    }

    /*
    *
    * 点击事件
    *
    * */
    private void initData() {
        Ligoodass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Imgoodass.setImageResource(R.drawable.icon_pj_hp_sel);
                Imcenterass.setImageResource(R.drawable.icon_pj_zp_nor);
                Imlowass.setImageResource(R.drawable.icon_pj_cp_nor);
                ass = 1;

            }
        });
        Licenterass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Imgoodass.setImageResource(R.drawable.icon_pj_hp_nor);
                Imcenterass.setImageResource(R.drawable.icon_pj_zp_sel);
                Imlowass.setImageResource(R.drawable.icon_pj_cp_nor);
                ass = 2;
            }
        });
        Lilowass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Imgoodass.setImageResource(R.drawable.icon_pj_hp_nor);
                Imcenterass.setImageResource(R.drawable.icon_pj_zp_nor);
                Imlowass.setImageResource(R.drawable.icon_pj_cp_sel);
                ass =3;
            }
        });
        Imback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });




        materialRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean b) {
                if (rating == 0f){
                    mater = 0;
                }else if (rating == 1f){
                    mater = 1;
                }else if (rating == 2f){
                    mater = 2;
                }else if (rating == 3f){
                    mater = 3;
                }else if (rating == 4f){
                    mater = 4;
                }else if (rating == 5f){
                    mater = 5;
                }

            }
        });

        materialRatingBar2.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean b) {
                if (rating == 0f){
                    mater2 = 0;
                }else if (rating == 1f){
                    mater2 = 1;
                }else if (rating == 2f){
                    mater2 = 2;
                }else if (rating == 3f){
                    mater2 = 3;
                }else if (rating == 4f){
                    mater2 = 4;
                }else if (rating == 5f){
                    mater2 = 5;
                }

            }
        });

        materialRatingBar3.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean b) {
                if (rating == 0f){
                    mater3 = 0;
                }else if (rating == 1f){
                    mater3 = 1;
                }else if (rating == 2f){
                    mater3 = 2;
                }else if (rating == 3f){
                    mater3 = 3;
                }else if (rating == 4f){
                    mater3 = 4;
                }else if (rating == 5f){
                    mater3 = 5;
                }

            }
        });
        try {
            send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    text = ed_content.getText().toString();
                    if (text.equals("")){
                        ToastUtils.showShort("请填写完整");
                    }else {
                        Intent intent = getIntent();
                        String orderId = intent.getStringExtra("orderId");
                        String content =  ed_content.getText().toString();
                        String skuid = intent.getStringExtra("skuid");
                        Log.e("评价","http://develop.jhjvip.cn:8080/mall/mobile/goods/skuFeedback/add"+
                                "?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)
                                +"&appId="+AUniqueIdentifier.getDeviceId(AssessActivity.this)+"&reorder=1&skuId="+skuid
                                +"&content="+text+"&overallLevel="+ass+"&wuliuLevel="
                                +mater+"&descLevel="+mater2+"&serverLevel="+mater3+"&orderNo="+orderId);
                        OkHttpUtils.post().url(ApiManager.BASE_NIUPAI_URL+"goods/skuFeedback/add")
                                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                                .addParams("appId", AUniqueIdentifier.getDeviceId(AssessActivity.this))
                                .addParams("reorder","1")
                                .addParams("skuId",skuid)
                                .addParams("content",text)
                                .addParams("orderNo",orderId)
                                .addParams("overallLevel",ass+"")
                                .addParams("wuliuLevel",mater+"")
                                 .addParams("descLevel",mater2+"")
                                .addParams("serverLevel",mater3+"")

                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {

                                    }

                                    @Override
                                    public void onResponse(String response) {

                                        Log.e("asssHB",response);
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            String code = jsonObject.getString("code");
                                            if (code.equals("10000")){ //请求成功！
                                                AlertDialog alertDialog = new AlertDialog.Builder(AssessActivity.this).setTitle("提示").setMessage("评价成功")
                                                        .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                                            @Override
                                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                                finish();
                                                            }
                                                        }).create();
                                                alertDialog.show();
                                            }else {
//                                                ToastUtils.showShort("保存失败，请稍后再试...");
                                                ToastUtils.showShort(jsonObject.getString("msg"));
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                    }


                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }



    }

    @Override
    public void onActivityResult(int requestCode, int arg1, Intent arg2) {
        super.onActivityResult(requestCode, arg1, arg2);
        selectImageView.onActivityResult(requestCode, arg1, arg2);
    }
}